package com.example.text.helper

import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.example.text.R
import com.example.text.databinding.ItemGoodsRowInfoBinding
import com.example.text.ui.mall.adepter.CommonVDBRecyclerAdapter
import com.example.text.ui.mall.model.GoodsFormInfoItem

//
//
//
//
//import android.animation.Animator
//import android.animation.AnimatorListenerAdapter
//import android.animation.AnimatorSet
//import android.animation.ObjectAnimator
//import android.graphics.Color
//import android.os.Handler
//import android.os.Looper
//import android.view.LayoutInflater
//import android.view.View
//import android.widget.LinearLayout
//import androidx.constraintlayout.widget.ConstraintLayout
//import androidx.recyclerview.widget.RecyclerView
//import com.bigkoo.convenientbanner.ConvenientBanner
//
//import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
//
//
///** 首页摩托车系列 */
//class MallHomeMotorSeriesHelper(private val rv: RecyclerView, private val onClick: ((view: View, item: MallMotorSeries) -> Unit)?) {
//    private var mAdapter: CommonVDBRecyclerAdapter<MallMotorSeries, ItemMallMotorSeriesBinding>? = null
//    private var mItems = ArrayList<MallMotorSeries>()
//
//    fun display(dataList: ArrayList<MallMotorSeries>, removeLast: Boolean = true) {
//        mItems.clear()
//        mItems.addAll(dataList)
//        if (removeLast) mItems.removeLast()
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<MallMotorSeries, ItemMallMotorSeriesBinding>(R.layout.item_mall_motor_series, mItems) {
//                override fun convert(holder: BaseDataBindingHolder<ItemMallMotorSeriesBinding>, item: MallMotorSeries) {
//                    val binding = holder.dataBinding
//                    binding?.apply {
//                        this.item = item
//                        tvSeriesName.isSelected = item.checked
//                        tvSeriesName.setOnClickListener {
//                            onClick?.invoke(it, item)
//                            onChecked(item)
//                        }
//                        executePendingBindings()
//                    }
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.notifyDataSetChanged()
//        }
//    }
//
//    fun onChecked(item: MallMotorSeries?) {
//        mItems.forEach { it.checked = false }
//        item?.checked = true
//        mAdapter?.notifyDataSetChanged()
//    }
//}
//
//class MallHomeMotorSeriesHSVHelper(
//    private val hsv: LinearLayout, private val layoutInflater: LayoutInflater,
//    private val onClick: ((view: View, item: MallMotorSeries) -> Unit)?
//) {
//    // private var mAdapter: CommonVDBRecyclerAdapter<MallMotorSeries, ItemMallMotorSeriesBinding>? = null
//    private var mItems = ArrayList<MallMotorSeries>()
//
//    private val childList = mutableListOf<ItemMallMotorSeriesBinding>()
//
//    fun display(dataList: ArrayList<MallMotorSeries>, removeLast: Boolean = true) {
//        mItems.clear()
//        mItems.addAll(dataList)
//        if (removeLast) mItems.removeLast()
//        hsv.removeAllViews()
//        childList.clear()
//        mItems.forEach { item ->
//            val child = ItemMallMotorSeriesBinding.inflate(layoutInflater)
//            child.apply {
//                this.item = item
//                tvSeriesName.isSelected = item.checked
//                tvSeriesName.setOnClickListener {
//                    onClick?.invoke(it, item)
//                    onChecked(item)
//                }
//            }
//            hsv.addView(child.root)
//            childList.add(child)
//        }
//    }
//
//    fun onChecked(item: MallMotorSeries?) {
//        mItems.forEach { it.checked = false }
//        item?.checked = true
//        childList.forEach {
//            it.tvSeriesName.isSelected = it.item?.checked == true
//        }
//    }
//}
//
///** 商城首页-热门活动列表 */
//class HomeActListHelper(private val rv: RecyclerView) {
//
//    private var mAdapter: CommonVDBRecyclerAdapter<MallActivityItem, ItemMallHomeActChildBinding>? = null
//
//    fun display(dataList: ArrayList<MallActivityItem>) {
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<MallActivityItem, ItemMallHomeActChildBinding>(R.layout.item_mall_home_act_child, dataList) {
//                override fun convert(holder: BaseDataBindingHolder<ItemMallHomeActChildBinding>, item: MallActivityItem) {
//                    val childBinding = holder.dataBinding
//                    childBinding?.apply {
//                        this.item = item
//
//                        container.setOnClickListener {
//                            if (!item.hasExpired()) {
//                                if (item.isGoods) {
//                                    // 跳转到详情
//                                    ActDetailActivity.jump(actId = item.id, dataId = item.dataId)
//                                } else {
//                                    CommonWebActivity.jump(title = item.name, url = item.linkUrl ?: "")
//                                }
//
//                                // c端b端-商城热门活动点击更新浏览量
//                                //postEvent(NotifyActivityPvUpdate(item.id))
//                            }
//                        }
//                        containerMore.setOnClickListener {
//                            // 更多活动
//                            ActListActivity.jump()
//                        }
//                        executePendingBindings()
//                    }
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.setNewInstance(dataList)
//        }
//    }
//}
//
///** 商城搜索筛选品牌 */
//class MallSearchBrandHelper(private val rv: RecyclerView, private val onClick: ((item: MallMotorBrandItem) -> Unit)?) {
//    private var mAdapter: CommonVDBRecyclerAdapter<MallMotorBrandItem, ItemMallMotorBrandBinding>? = null
//    private val mItems = arrayListOf<MallMotorBrandItem>()
//    var checkedItem: MallMotorBrandItem? = null
//
//    fun hasInit() = mAdapter != null
//
//    fun reset() {
//        checkedItem = null
//        mItems.forEach { it.checked = false }
//        mAdapter?.notifyDataSetChanged()
//    }
//
//    fun display(dataList: ArrayList<MallMotorBrandItem>) {
//        mItems.clear()
//        mItems.addAll(dataList)
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<MallMotorBrandItem, ItemMallMotorBrandBinding>(R.layout.item_mall_motor_brand, mItems) {
//                override fun convert(holder: BaseDataBindingHolder<ItemMallMotorBrandBinding>, item: MallMotorBrandItem) {
//                    val binding = holder.dataBinding
//                    binding?.item = item
//                    binding?.tvBrandName?.run {
//                        setTextColor(
//                            if (item.checked) Color.parseColor("#d8000f")
//                            else Color.parseColor("#333333")
//                        )
//                        setBackgroundResource(
//                            if (item.checked) R.drawable.bg_circle_theme_with_theme_line
//                            else R.drawable.bg_circle_gray_light
//                        )
//                    }
//                    binding?.tvBrandName?.setOnClickListener { _ ->
//                        dataList.forEach { it.checked = false }
//                        item.checked = true
//                        mAdapter?.notifyDataSetChanged()
//                        checkedItem = item
//                        onClick?.invoke(item)
//                    }
//                    binding?.executePendingBindings()
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.notifyDataSetChanged()
//        }
//    }
//}
//
///** 商城搜索筛选品牌 */
//class MallFilterBrandHelper(private val rv: RecyclerView, private val onClick: ((item: MallMotorBrandItem) -> Unit)?) {
//    private var mAdapter: CommonVDBRecyclerAdapter<MallMotorBrandItem, ItemMallMotorBrandHBinding>? = null
//    private val mItems = arrayListOf<MallMotorBrandItem>()
//    var checkedItem: MallMotorBrandItem? = null
//
//    fun hasInit() = mAdapter != null
//
//    fun reset() {
//        checkedItem = null
//        mItems.forEach { it.checked = false }
//        mAdapter?.notifyDataSetChanged()
//    }
//
//    fun checkedId(): Long {
//        return mItems.find { it.checked }?.id ?: 0L
//    }
//
//    fun display(dataList: ArrayList<MallMotorBrandItem>) {
//        mItems.clear()
//        mItems.addAll(dataList)
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<MallMotorBrandItem, ItemMallMotorBrandHBinding>(R.layout.item_mall_motor_brand_h, mItems) {
//                override fun convert(holder: BaseDataBindingHolder<ItemMallMotorBrandHBinding>, item: MallMotorBrandItem) {
//                    val binding = holder.dataBinding
//                    binding?.item = item
//                    binding?.tvBrandName?.run {
//                        setTextColor(
//                            if (item.checked) Color.parseColor("#d8000f")
//                            else Color.parseColor("#333333")
//                        )
//                        setBackgroundResource(
//                            if (item.checked) R.drawable.bg_circle_theme_with_theme_line
//                            else R.drawable.bg_circle_gray_light
//                        )
//                    }
//                    binding?.tvBrandName?.setOnClickListener { _ ->
//                        dataList.forEach { it.checked = false }
//                        item.checked = true
//                        mAdapter?.notifyDataSetChanged()
//                        checkedItem = item
//                        onClick?.invoke(item)
//                    }
//                    binding?.executePendingBindings()
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.notifyDataSetChanged()
//        }
//    }
//}
//
/** 商品详情-保障/已选/参数/物流 */
class GoodsDetailServiceHelper(private val rv: RecyclerView, private val onClick: ((item: GoodsFormInfoItem) -> Unit)?) {
    private var mAdapter: CommonVDBRecyclerAdapter<GoodsFormInfoItem, ItemGoodsRowInfoBinding>? = null
    fun notifyDataSetChanged() {
        mAdapter?.notifyDataSetChanged()
    }

    fun display(dataList: ArrayList<GoodsFormInfoItem>) {
        if (mAdapter == null) {
            mAdapter = object : CommonVDBRecyclerAdapter<GoodsFormInfoItem, ItemGoodsRowInfoBinding>(
                R.layout.item_goods_row_info, dataList) {
                override fun convert(holder: BaseDataBindingHolder<ItemGoodsRowInfoBinding>, item: GoodsFormInfoItem) {
                    val binding = holder.dataBinding
                    binding?.item = item
                    binding?.container?.setOnClickListener {
                        onClick?.invoke(item)
                    }
                    binding?.executePendingBindings()
                }
            }
            rv.adapter = mAdapter
        } else {
            mAdapter?.setNewInstance(dataList)
        }
    }
}

///** 商品详情-为你推荐 */
//class GoodsDetailRecommendHelper(private val rv: RecyclerView) {
//    private var mAdapter: CommonVDBRecyclerAdapter<MallGoods, ItemMallGoodsRecommendBinding>? = null
//
//    fun display(dataList: ArrayList<MallGoods>) {
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<MallGoods, ItemMallGoodsRecommendBinding>(R.layout.item_mall_goods_recommend, dataList) {
//                override fun convert(holder: BaseDataBindingHolder<ItemMallGoodsRecommendBinding>, item: MallGoods) {
//                    val binding = holder.dataBinding
//                    binding?.item = item
//                    binding?.container?.setOnClickListener {
//                        // onClick?.invoke(item)
//                        GoodsDetailActivity.jump(item.id)
//                    }
//                    binding?.executePendingBindings()
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.setNewInstance(dataList)
//        }
//    }
//}
//
///** 商品详情-商品评价 */
//class GoodsDetailEvaluationHelper(private val rv: RecyclerView) {
//    private var mAdapter: CommonVDBRecyclerAdapter<MallEvaluation, ItemMallEvaluation4pBinding>? = null
//
//    fun display(dataList: ArrayList<MallEvaluation>, onImageClick: ((Int, Int, List<String>, List<Info>?) -> Unit)? = null) {
//        dataList.lastOrNull()?.last = true
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<MallEvaluation, ItemMallEvaluation4pBinding>(R.layout.item_mall_evaluation_4p, dataList) {
//                override fun convert(holder: BaseDataBindingHolder<ItemMallEvaluation4pBinding>, item: MallEvaluation) {
//                    val binding = holder.dataBinding
//                    binding?.apply {
//                        this.item = item
//                        // 展示图片
//                        // nineGridView.setPicsList(TYPE_0V4P, item.transImgs(4) ?: emptyList(), item.transImgs())
//                        nineGridView.setPicsList(TYPE_0V4P, item.transImgs() ?: emptyList(), item.transImgs())
//                        onImageClick?.let {
//                            nineGridView.setOnItemClickListener(onImageClick)
//                        }
//                        nineGridView.initView(holder.absoluteAdapterPosition)
//
//                        executePendingBindings()
//                    }
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.setNewInstance(dataList)
//        }
//    }
//}
//
///** 商品详情-介绍图片 */
//class GoodsDetailPicsHelper(private val layout: LinearLayout) {
//    fun display(layoutInflater: LayoutInflater, dataList: ArrayList<String>) {
//        layout.removeAllViews()
//        dataList.forEach {
//            val child = ItemMallGoodsPicBinding.inflate(layoutInflater)
//            child.pic = it
//            layout.addView(child.root)
//        }
//    }
//}
//
//
///** 活动详情-爆款推荐 */
//class BoomGoodsHelper(private val rv: RecyclerView, private val onClick: ((item: MallGoods) -> Unit)?) {
//    private var mAdapter: CommonVDBRecyclerAdapter<MallGoods, ItemMallGoodsBoomHBinding>? = null
//
//    fun display(dataList: ArrayList<MallGoods>) {
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<MallGoods, ItemMallGoodsBoomHBinding>(R.layout.item_mall_goods_boom_h, dataList) {
//                override fun convert(holder: BaseDataBindingHolder<ItemMallGoodsBoomHBinding>, item: MallGoods) {
//                    val binding = holder.dataBinding
//                    binding?.item = item
//                    binding?.container?.setOnClickListener {
//                        onClick?.invoke(item)
//                    }
//                    binding?.executePendingBindings()
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.setNewInstance(dataList)
//        }
//    }
//}
//
///** 活动详情-热卖榜单 */
//class HotRankGoodsHelper(private val rv: RecyclerView) {
//    private var mAdapter: CommonVDBRecyclerAdapter<MallGoods, ItemMallGoodsHotRankBinding>? = null
//
//    fun display(dataList: ArrayList<MallGoods>) {
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<MallGoods, ItemMallGoodsHotRankBinding>(R.layout.item_mall_goods_hot_rank, dataList) {
//                override fun convert(holder: BaseDataBindingHolder<ItemMallGoodsHotRankBinding>, item: MallGoods) {
//                    val binding = holder.dataBinding
//                    binding?.item = item
//                    binding?.container?.setOnClickListener {
//                        // onClick?.invoke(item)
//                        GoodsDetailActivity.jump(item.id)
//                    }
//                    binding?.executePendingBindings()
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.setNewInstance(dataList)
//        }
//    }
//}
//
//class GoodsDetailRecentlyHelper2(private val parent: ConstraintLayout) {
//    private val delay = 4000L
//    private var position = 0
//    private var count = 0
//    private val offsetY = 32f.dp
//    private var viewList = mutableListOf<View>()
//    private var handler: Handler? = null
//
//    fun stop() {
//        handler?.removeCallbacksAndMessages(null)
//    }
//
//    fun display(banners: List<RecentlyPurchased>) {
//        position = 0
//        count = banners.size
//        viewList.clear()
//        parent.removeAllViews()
//        handler?.removeCallbacksAndMessages(null)
//        if (handler == null)
//            handler = Handler(Looper.getMainLooper())
//
//        banners.forEachIndexed { index, it ->
//            val binding = ComponentRecentlyBuyBinding.inflate(LayoutInflater.from(appContext))
//            binding.item = it
//            viewList.add(binding.root)
//            // binding.root.visibility = if (index == position) View.VISIBLE else View.INVISIBLE
//            if (index > 0)
//                binding.root.translationY = offsetY
//            parent.addView(binding.root)
//        }
//        if (banners.size > 1) {
//            handler?.postDelayed({
//                startAnim()
//            }, delay)
//        }
//    }
//
//    private fun startAnim() {
//        if (position >= count)
//            position = 0
//        val view1 = viewList[position]
//        val view2 = viewList.getOrNull(position + 1) ?: viewList[0]
//        val anim1 = ObjectAnimator.ofFloat(view1, "translationY", view1.translationY, offsetY * -1)
//        val anim2 = ObjectAnimator.ofFloat(view2, "translationY", view2.translationY, 0f)
//
//        val animSet = AnimatorSet()
//        animSet.duration = 222
//        animSet.playTogether(anim1, anim2)
//        animSet.start()
//        animSet.addListener(object : AnimatorListenerAdapter() {
//            override fun onAnimationEnd(animation: Animator) {
//                // 众神归为
//                view1.translationY = offsetY
//            }
//        })
//        position++
//        handler?.postDelayed({
//            startAnim()
//        }, 3222)
//    }
//}
//
//class GoodsDetailRecentlyHelper(private val banner: ConvenientBanner<Any>) {
//    private var hasInitBanner = false
//    private var bannerIsTurning = false
//
//    fun convertBanner(banners: List<RecentlyPurchased>) {
//        if (hasInitBanner) {// 已初始化过Banner
//            banner.apply {
//                setPages({ GoodsRecentlyBuyBannerHolder() }, banners)
//
//                if (!bannerIsTurning) {
//                    bannerIsTurning = true
//                    banner.startTurning(3_000)
//                }
//                notifyDataSetChanged()
//            }
//        } else {// 未初始化过Banner
//            hasInitBanner = true
//            banner.apply {
//                setPages({ GoodsRecentlyBuyBannerHolder() }, banners)
//            }
//            bannerIsTurning = true
//            banner.startTurning(3_000)
//        }
//    }
//}
//
//class DouHomeTaskListHelper(
//    private val rv: RecyclerView,
//    private val onClick: ((item: LongDouTask) -> Unit)?
//) {
//    private var mAdapter: CommonVDBRecyclerAdapter<LongDouTask, ItemLongdouTaskHomeBinding>? = null
//
//    fun display(dataList: ArrayList<LongDouTask>) {
//        dataList.lastOrNull()?.last = true
//
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<LongDouTask, ItemLongdouTaskHomeBinding>(R.layout.item_longdou_task_home, dataList) {
//                override fun convert(holder: BaseDataBindingHolder<ItemLongdouTaskHomeBinding>, item: LongDouTask) {
//                    val binding = holder.dataBinding
//                    binding?.item = item
//                    binding?.btnTodo?.isEnabled = !item.finishMark
//                    binding?.btnTodo?.setOnClickListener {
//                        onClick?.invoke(item)
//                        // GoodsDetailActivity.jump(item.id)
//                    }
//                    binding?.executePendingBindings()
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.setNewInstance(dataList)
//        }
//    }
//}
//
///** 申请售后-商品列表 */
//class ApplyAfterSalesGoodsHelper(private val rv: RecyclerView) {
//    private var mAdapter: CommonVDBRecyclerAdapter<MallOrderDetailResp, ItemMallGoodsAfterSalesBinding>? = null
//
//    fun notifyData() {
//        mAdapter?.notifyDataSetChanged()
//    }
//
//    fun display(dataList: ArrayList<MallOrderDetailResp>, onClick: (() -> Unit)? = null) {
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<MallOrderDetailResp, ItemMallGoodsAfterSalesBinding>(R.layout.item_mall_goods_after_sales, dataList) {
//                override fun convert(holder: BaseDataBindingHolder<ItemMallGoodsAfterSalesBinding>, item: MallOrderDetailResp) {
//                    val binding = holder.dataBinding
//                    binding?.apply {
//                        // 申请页面不展示上次的状态
//                        item.afterSalesStatusDesc = ""
//
//                        this.item = item
//                        checkbox.isSelected = item.checked
//                        container.setOnClickListener {
//                            if (item.onEdit) {
//                                item.checked = !item.checked
//                                checkbox.isSelected = item.checked
//                                notifyData()
//                            }
//                            onClick?.invoke()
//                        }
//                        executePendingBindings()
//                    }
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.notifyDataSetChanged()
//        }
//    }
//}
//
//// 龙豆签到记录
//class DouSignHistoryHelper(private val rv: RecyclerView, private val onClick: () -> Unit) {
//    private var mAdapter: CommonVDBRecyclerAdapter<SignDayDTO, ItemDouSignUpHistoryBinding>? = null
//
//    fun display(dataList: ArrayList<SignDayDTO>) {
//        if (mAdapter == null) {
//            mAdapter = object : CommonVDBRecyclerAdapter<SignDayDTO, ItemDouSignUpHistoryBinding>(R.layout.item_dou_sign_up_history, dataList) {
//                override fun convert(holder: BaseDataBindingHolder<ItemDouSignUpHistoryBinding>, item: SignDayDTO) {
//                    val binding = holder.dataBinding
//                    binding?.apply {
//                        this.item = item
//                        // 豆子状态
//                        this.ivDou.isEnabled = item.ifEnable() || item.ifToday()
//                        this.ivDou.isSelected = item.ifSigned()
//                        // 背景状态
//                        this.bgState.isEnabled = item.ifEnable() || item.ifToday()
//                        this.bgState.isSelected = item.ifSigned() && !item.ifToday()
//                        // 弹窗
//                        container.setOnClickListener {
//                            if (item.ifToday() && item.ifUnSigned()) {
//                                onClick.invoke()
//                            }
//                        }
//                        executePendingBindings()
//                    }
//                }
//            }
//            rv.adapter = mAdapter
//        } else {
//            mAdapter?.setNewInstance(dataList)
//        }
//    }
//}
